from turtle import up

import cv2 as cv
from matplotlib import pyplot as plt


def build_gaussian_pyramid(image, levels):
    pyramid = [image]
    temp = image
    for i in range(levels):
        blurred = cv.GaussianBlur(temp, (5, 5), 0)
        down_sampled = cv.resize(
            blurred,
            (temp.shape[1] // 2, temp.shape[0] // 2),
            interpolation=cv.INTER_LINEAR,
        )
        pyramid.append(down_sampled)
        temp = down_sampled
    return pyramid


def build_laplacian_pyramid(image, levels):
    gaussian_pyramid = build_gaussian_pyramid(image, levels)
    laplacian_pyramid = []
    for i in range(levels):
        up_sampled = cv.resize(
            gaussian_pyramid[i + 1],
            (gaussian_pyramid[i].shape[1], gaussian_pyramid[i].shape[0]),
            interpolation=cv.INTER_LINEAR,
        )
        laplacian_layer = cv.subtract(gaussian_pyramid[i], up_sampled)
        laplacian_pyramid.append(laplacian_layer)
    return laplacian_pyramid


image = cv.imread("images/lena.jpg", cv.IMREAD_GRAYSCALE)
pyramid = build_laplacian_pyramid(image, 3)

plt.rcParams["font.sans-serif"] = ["Times New Roman"]
plt.figure(figsize=(12, 6))
for i in range(len(pyramid)):
    plt.subplot(1, len(pyramid), i + 1)
    plt.imshow(pyramid[i], cmap="gray")
    plt.title("Level " + str(i))
    plt.axis("off")
plt.tight_layout()
plt.show()
